
import { createRouter, createWebHistory,createWebHashHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import { useAppStore } from '@/store/modules/app';
import { isEmpty } from "@/utils";
import { close, start } from '@/utils/nporgress';
const routes: Array<RouteRecordRaw> = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/account/login.vue"),
  },
  {
    path: "/404",
    name: "404",
    component: () => import("@/views/common/404.vue"),
  },

  // {
  //     path: "/",
  //     component: () => import("@/layout/index.vue"),
  //     children: [
  //       {
  //         meta: {title: 'dashboard',menu: true},
  //         path: "/",
  //         component: () => import("@/views/dashboard/index.vue"),
  //       }
  //     ]
  // },
  // {
  //   path: "/:catchAll(.*)",
  //   redirect: '/404',
  // }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior(to,from,savePosition){
    if(savePosition){
      return savePosition;
    }else{
      return {top:0};
    }
  }
});



router.beforeEach((to, from, next) => {
  start();
  const appStore = useAppStore();
  if (appStore.routeHasMounted==true && !isEmpty(appStore.token)){
    appStore.getAddNewRoute().then((res: Boolean) => {
      appStore.addRouteHas(false);
      console.info("------------------------");
      console.info("加载路由",res,router.getRoutes());
      console.info("------------------------");
      next({ ...to, replace: true });
    })
  }else{
      if(to.name!='login' && appStore.menuList.length==0){
        router.push({
            path: '/login',
            query: {
                T:new Date().getTime()
            }
        });
        next();
      }else{
        if(to.meta.menu==true){
          appStore.addMenuActive(setString(to.name));
          appStore.addNav({name:to.meta.title,menuId:setString(to.name),path:to.path});
        }
        next();
      }
  }
});
const setString = (event:any) =>{
  return event.toString();
}
router.afterEach(() => {
  close()
})

export default router
